package it.mp.multitask.adapter;

import it.mp.multitask.R;
import it.mp.multitask.factory.Style;
import it.mp.multitask.utility.PatternEntry;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class BackgroundAdapter extends ArrayAdapter<String> {
	private String[] chiavi;
	private String[] valori;
	private Context mContext;
	private int standardLayout;

	public BackgroundAdapter(Context context, String[] chiavi, String[] valori,int standardLayout) {
		super(context, R.id.list_item_text, chiavi);
		this.chiavi = chiavi;
		this.valori = valori;
		mContext = context;
		this.standardLayout=standardLayout;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		if (v == null) {
			LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.background_item, null);
		}

		String ic = chiavi[position];
		if (ic != null) {
			BitmapDrawable bd = null;
			if (standardLayout==Style.LAYOUT_EXTENDED){
				bd=PatternEntry.createDrawable(mContext.getResources(), ic);
			}else{
				bd=PatternEntry.createDrawable(mContext.getResources(), PatternEntry.decodePattern(ic,standardLayout),standardLayout);
			}
			if (bd == null) {
				v.findViewById(R.id.list_item_text).setBackgroundResource(PatternEntry.decodePattern(ic,standardLayout));
			} else {
				v.findViewById(R.id.list_item_text).setBackgroundDrawable(bd);
			}

			((TextView) v.findViewById(R.id.list_item_text)).setText(valori[position]);
			if (valori[position].equals("Transparent")) {
				v.findViewById(R.id.list_item_text).setBackgroundResource(android.R.color.black);
			}
		}

		return v;
	}

	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return getView(position, convertView, parent);
	}
}
